<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>学生信息管理系统</title>
    <link rel="stylesheet" href="lib/bootstrap.min.css">
    <link rel="stylesheet" href="lib/datepicker/skin/WdatePicker.css">
    <style>
        #template {
            display: none;
        }
        
        * {
            padding: 0;
            margin: 0;
        }
        
        html,
        body {
            width: 100%;
            height: 100%;
        }
        
        .box {
            width: 95%;
            height: 100%;
            margin: 0 auto;
        }
        
        .table thead tr th:nth-of-type(1),
        .table thead tr th:nth-of-type(2),
        .table thead tr th:nth-of-type(3),
        .table thead tr th:nth-of-type(4) {
            width: 130px;
        }
        
        .table thead tr th:nth-of-type(5),
        .table thead tr th:nth-of-type(6) {
            width: 170px;
        }
        
        .nowUser {
            height: 50px;
            width: 100%;
            /* background-color: pink; */
            line-height: 50px;
            font-size: 18px;
        }
        
        .nowUser>span {
            display: inline-block;
            float: left;
        }
        
        .nowUser>span:nth-of-type(3) {
            margin-right: 20px;
        }
        
        .nowUser>span:nth-of-type(4) {
            color: blue;
            cursor: pointer;
        }
        
        .delete-wrap {
            height: 100%;
            width: 100%;
            position: fixed;
            background-color: rgba(0, 0, 0, .5);
            top: 0;
            left: 0;
            display: none;
        }
        
        .delete-popup {
            height: 200px;
            width: 280px;
            background-color: #fff;
            border-radius: 10px;
            position: fixed;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            display: none;
        }
        
        .delete-popup>p {
            height: 100px;
            /* background-color: pink; */
            text-align: center;
            line-height: 140px;
            font-size: 16px;
        }
        
        .delete-popup .delete-true,
        .delete-popup .delete-false {
            margin-top: 20px;
        }
        
        .delete-popup .delete-true {
            margin-left: 60px;
            margin-right: 40px;
        }
        
        .addStudent {
            float: right;
            margin-right: 40px;
        }
        
        .add-popup {
            /* height: 400px; */
            padding: 30px 20px;
            width: 550px;
            background-color: #fff;
            border-radius: 10px;
            position: fixed;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            display: none;
        }
        
        .add-popup>h3 {
            /* background-color: pink; */
            height: 40px;
            line-height: 40px;
            text-indent: 30px;
            margin-bottom: 15px;
        }
        
        .radio-inline {
            margin-left: 16px;
        }
        
        select {
            margin-top: 8px;
        }
        
        #province {
            margin-left: 16px;
        }
        
        .add-true {
            margin-top: 10px;
            margin-left: 60px;
        }
        
        .add-false {
            margin-top: 10px;
            margin-left: 90px;
        }
        
        .false {
            font-size: 26px;
            position: absolute;
            top: 10px;
            right: 20px;
            cursor: pointer;
            transform: scale(1);
            z-index: 2;
        }
        
        .false:hover {
            transform: scale(1.1);
            transition: transform .6s;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="addStudent">
            <button class="btn btn-primary data-add">+ 添加学生</button>
        </div>
        <h2>学生信息管理系统</h2>
        <hr>
        <div class="nowUser">
            <span> 当前用户:</span>
            <span id="username"></span>
            <span id="usertype"></span>
            <span id="quit">[退出登录]</span>
        </div>
        <hr>
        <!-- 表结构 -->
        <table class="table">
            <thead>
                <tr>
                    <th>学号</th>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>年龄</th>
                    <th>籍贯</th>
                    <th>入学时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody class="data-student">
                <tr id="template">
                    <td class="data-id"></td>
                    <td class="data-name"></td>
                    <td class="data-sex"></td>
                    <td class="data-age"></td>
                    <td class="data-citys"></td>
                    <td class="data-joinDate"></td>
                    <td>
                        <button class="btn btn-danger delete data-delete">删除学员</button>
                        <button class="btn btn-warning edit data-edit">编辑学员信息</button>
                        <button class="btn btn-primary  data-score">查看成绩</button>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>

    <!-- 删除按钮的遮罩层 -->
    <div class="delete-wrap"></div>
    <div class="delete-popup">
        <span class="false">×</span>
        <p>确认删除吗？</p>
        <button type="button" class="btn btn-danger delete-true">确认</button>
        <button type="button" class="btn btn-warning delete-false">取消</button>
    </div>

    <!-- 添加按钮的遮罩层 -->
    <div class="add-popup">
        <span class="false">×</span>
        <h3>
            添加学生
        </h3>
        <div class="form-horizontal">
            <div class="form-group">
                <label for="name" class="col-sm-2 control-label">姓名</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="name" placeholder="请输入学生姓名">
                </div>
            </div>

            <div class="form-group">
                <label for="age" class="col-sm-2 control-label">年龄</label>
                <div class="col-sm-10">
                    <input type="number" class="form-control" id="age" placeholder="请输入年龄">
                </div>
            </div>

            <div class="form-group">
                <label for="sex" class="col-sm-2 control-label">性别</label>
                <label class="radio-inline">
                    <input type="radio" name="sex" id="sex-boy" checked>男
                </label>
                <label class="radio-inline">
                    <input type="radio" name="sex" id="sex-girl">女
                </label>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label">所在城市</label>
                <select name="" id="province"></select>
                <select name="" id="city"></select>
                <select name="" id="area"></select>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label">入学时间</label>
                <div class="col-sm-10">
                    <input type="input" class="form-control" id="data-joinDate" placeholder="入学日期">
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-danger add-true">确认添加</button>
                    <button type="button" class="btn btn-warning add-false">取消</button>
                </div>
            </div>
        </div>

    </div>

    <script src="lib/jquery.min.js"></script>
    <script src="lib/citys.js"></script>
    <script src="lib/datepicker/WdatePicker.js"></script>
    <script src="lib/common.js"></script>

    <script>
        //登录验证
        let userInfo = sessionStorage.getItem("userInfo") || document.cookie.replace("userInfo=", "");
        if (!userInfo) {
            location.href = `${address}/login.html`;
        }

        let template = $("#template");
        let tbody = $(".data-student");
        let province = $("#province");
        let city = $("#city");
        let area = $("#area");

        //省市区三级联动
        citys.forEach(item => {
            let option = $(`<option>${item.provinceName}</option>`);
            option.appendTo(province);
        })

        citys[0].mallCityList.forEach(item => {
            let option = $(`<option>${item.cityName}</option>`);
            option.appendTo(city);
        })

        citys[0].mallCityList[0].mallAreaList.forEach(item => {
            let option = $(`<option>${item.areaName}</option>`);
            option.appendTo(area);
        })

        province.change(function() {
            updataCity.call(this);
            updataArea.call(city[0]);
        })
        city.change(function() {
            updataArea.call(this);
        })

        function updataCity() {
            city.html("");
            //返回当前选中的选项在其select标签中的下标
            cityList = citys[this.selectedIndex].mallCityList;
            cityList.forEach(item => {
                let option = $(`<option>${item.cityName}</option>`);
                city.append(option);
            })
        }

        function updataArea() {
            area.html("");
            let areaList = cityList[this.selectedIndex].mallAreaList;
            areaList.forEach(item => {
                let option = $(`<option>${item.areaName}</option>`);
                area.append(option);
            })
        }

        //查看操作
        $.ajax({
            url: `${address}/getStudents`,
            success(data) {
                data.forEach(item => {
                    let newItem = template.clone(true);
                    newItem.attr("id", "");
                    newItem.find(".data-id").html(item.id);
                    newItem.find(".data-name").html(item.name);
                    newItem.find(".data-sex").html(item.sex ? "女" : "男");
                    newItem.find(".data-age").html(item.age);
                    newItem.find(".data-citys").html(item.citys);
                    newItem.find(".data-joinDate").html(item.joinDate);
                    //不同用户进入后角色不同
                    if ($("#usertype").html() === "老师") {
                        newItem.appendTo(tbody);
                    }
                    if ($("#usertype").html() === "同学") {
                        $(".data-delete").remove();
                        $(".data-edit").remove();
                        $(".data-add").remove();
                        newItem.appendTo(tbody);
                    }
                })
            }
        })

        //删除操作
        //可以给所有的删除按钮绑定事件
        //也可以给模板绑定事件,当克隆模板时，会连事件一起克隆，仍然会有删除按钮的效果
        template.find(".data-delete").click(function() {
            //要删除的行
            let target = $(this).closest("tr");
            $(".delete-wrap").fadeIn();
            $(".delete-popup").fadeIn();
            $(".delete-true").click(function() {
                $.ajax({
                    url: `${address}/delete`,
                    data: {
                        id: target.find(".data-id").html(),
                    },
                    success(data) {
                        if (data === "success") {
                            $(".delete-wrap").fadeOut();
                            $(".delete-popup").fadeOut();
                            target.fadeOut();
                        } else {
                            alert("删除失败");
                        }
                    }
                });

            })
            $(".delete-false").click(function() {
                $(".delete-wrap").fadeOut();
                $(".delete-popup").fadeOut();
            })
            $(".delete-popup .false").click(function() {
                $(".delete-wrap").fadeOut();
                $(".delete-popup").fadeOut();
            })
        })

        //日期插件
        $("#data-joinDate").click(function() {
            WdatePicker()
        })

        //添加操作
        $(".data-add").click(function() {
            $(".delete-wrap").fadeIn();
            $(".add-popup").fadeIn();
            $(".add-popup h3").html("添加学员");
            $("#name").val("");
            $("#age").val("");
            $("#data-joinDate").val("");
            $("#sex-boy")[0].checked = true;
            $("#sex-girl")[0].checked = false;
            $("#name").focus()
            $(".add-true").click(function() {
                if ($("#name").val() && $("#age").val() && $("#data-joinDate").val()) {
                    $.ajax({
                        url: `${address}/add`,
                        data: {
                            name: $("#name").val(),
                            age: $("#age").val(),
                            sex: $("#sex-boy")[0].checked ? 0 : 1,
                            city: $("#province").val() + $("#city").val() + $("#area").val(),
                            joinDate: $("#data-joinDate").val(),
                        },
                        success(data) {
                            if (data === "success") {
                                history.go(0);
                            } else {
                                alert("操作失败");
                            }
                        }
                    })
                    $(".delete-wrap").fadeOut();
                    $(".add-popup").fadeOut();
                } else {
                    alert("请输入完整信息")
                }
            })
            $(".add-false").click(function() {
                $(".delete-wrap").fadeOut();
                $(".add-popup").fadeOut();
            })
            $(".add-popup .false").click(function() {
                $(".delete-wrap").fadeOut();
                $(".add-popup").fadeOut();
            })
        })

        //编辑功能
        $(".data-edit").click(function() {
            $(".delete-wrap").fadeIn();
            $(".add-popup h3").html("编辑学员信息");
            let nowTr = $(this).closest("tr");
            $("#name").val(nowTr.find(".data-name").html());
            $("#age").val(nowTr.find(".data-age").html());
            if (nowTr.find(".data-sex").html() === "男") {
                $("#sex-boy")[0].checked = true;
                $("#sex-girl")[0].checked = false;
            } else {
                $("#sex-boy")[0].checked = false;
                $("#sex-girl")[0].checked = true;
            }
            $("#data-joinDate").val(nowTr.find(".data-joinDate").html());
            $(".add-true").html("确认编辑");
            $(".add-popup").fadeIn();
            $("#name").focus()
            $(".add-true").click(function() {
                if ($("#name").val() && $("#age").val() && $("#data-joinDate").val()) {
                    $.ajax({
                        url: `${address}/edit`,
                        data: {
                            id: nowTr.find(".data-id").html(),
                            name: $("#name").val(),
                            age: $("#age").val(),
                            sex: $("#sex-boy")[0].checked ? 0 : 1,
                            city: $("#province").val() + $("#city").val() + $("#area").val(),
                            joinDate: $("#data-joinDate").val(),
                        },
                        success(data) {
                            if (data === "success") {
                                history.go(0);
                            }
                        }
                    })
                } else {
                    alert("请输入完整信息");
                }
            })
            $(".add-false").click(function() {
                $(".delete-wrap").fadeOut();
                $(".add-popup").fadeOut();
            })
            $(".add-popup .false").click(function() {
                $(".delete-wrap").fadeOut();
                $(".add-popup").fadeOut();
            })
        })

        //回车和sec按键以增加用户体验
        window.onkeydown = function(event) {
            if (event.keyCode === 13) {
                $(".add-true").click();
                $(".delete-true").click();
            }
            if (event.keyCode === 27) {
                $(".add-false").click();
                $(".delete-false").click();
            }
        }

        //展示用户信息
        $("#username").html(JSON.parse(userInfo)[0].realname);
        $("#usertype").html(JSON.parse(userInfo)[0].usertype ? "同学" : "老师");

        //退出登录
        $("#quit").click(function() {
            //清除session
            sessionStorage.removeItem("userInfo");
            //清除cookie
            document.cookie = "userInfo=1;exprise=" + new Date("1970-01-01");
            location.href = `${address}/login.html`;
        })

        //查看学生成绩
        $(".data-score").click(function() {
            alert("该功能未完善")
            $.ajax({
                url: `${address}/score`,
                data: {
                    id: $(this).closest("tr").find(".data-id").html(),
                },
                success(data) {
                    console.log(data);
                }
            })
        })
    </script>
</body>

</html>